Page({
  data: {
    news: {},
    likeStatus: null, // 点赞状态
    likedList: [] // 已点赞新闻列表
  },
  onLoad: function (options) {
    const newsId = options.id;
    const baseUrl = 'http://39.104.24.231:8082';
    const url = `${baseUrl}/news/id/${newsId}`;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        console.log(res.data); 
        if (res.data.status === 200) {
          this.setData({
            news: res.data.data
          });
        } else {
          wx.showToast({
            title: '新闻获取失败: ' + res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败: ' + err.errMsg,
          icon: 'none'
        });
      }
    });
  },
 
  checkLikeStatus: function () {
    const userId = 1; 
    const newsId = 1;
    const baseUrl = 'http://39.104.24.231:8082';
    const checkUrl = baseUrl + '/agree/check';

    wx.request({
      url: checkUrl,
      method: 'POST',
      data: {
        USER_ID: userId,
        NEWS_ID: newsId
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          this.setData({
            likeStatus: res.data.data
          });
          wx.showToast({
            title: '点赞成功',
            icon:'success'
          });
        } else {
          wx.showToast({
            title: `点赞失败: ${res.data.message}`,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: `请求失败: ${err.errMsg}`,
          icon: 'none'
        });
      }
    });
  },

 // 改变点赞状态
changeLikeStatus: function () {
  const userId = 1;
  const newsId = 1;
  const currentStatus = this.data.likeStatus;
  const newStatus = !currentStatus; 
  const baseUrl = 'http://39.104.24.231:8082';
  const changeUrl = baseUrl + '/agree/agreeOrDisAgree';

  wx.request({
    url: changeUrl,
    method: 'POST',
    data: {
      USER_ID: userId,
      NEWS_ID: newsId,
      status: newStatus
    },
    header: {
      'content-type': 'application/json'
    },
    success: (res) => {
      if (res.data.status === 200) {
        this.setData({
          likeStatus: newStatus
        });
        wx.showToast({
          title: newStatus ? '点赞成功' : '取消点赞成功',
          icon: 'success'
        });
        if (newStatus) {
          const likedList = this.data.likedList;
          likedList.push(newsId);
          this.setData({
            likedList: likedList
          });
        } else {
          const likedList = this.data.likedList;
          const index = likedList.indexOf(newsId);
          if (index !== -1) {
            likedList.splice(index, 1);
            this.setData({
              likedList: likedList
            });
          }
        }
      } else {
        wx.showToast({
          title: `操作失败: ${res.data.message}`,
          icon: 'none'
        });
      }
    },
    fail: (err) => {
      wx.showToast({
        title: `请求失败: ${err.errMsg}`,
        icon: 'none'
      });
    }
  });
},

  // 请求已点赞列表
  getLikedList: function () {
    const userId = 1; 
    const baseUrl = 'http://39.104.24.231:8082';
    const listUrl = baseUrl + '/agree/getList';

    wx.request({
      url: listUrl,
      method: 'POST',
      data: {
        USER_ID: userId
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          this.setData({
            likedList: res.data.data
          });
          wx.showToast({
            title: '点赞成功',
            icon:'success'
          });
        } else {
          wx.showToast({
            title: `点赞失败: ${res.data.message}`,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: `请求失败: ${err.errMsg}`,
          icon: 'none'
        });
      }
    });
  },
});